package com.example.springboot.controller.user;

import com.example.springboot.bean.HttpResult;
import com.example.springboot.bean.VerifyCode;
import com.example.springboot.oauth2.entity.User;
import com.example.springboot.oauth2.jpa.UserJPA;
import com.example.springboot.utils.Constant;
import com.example.springboot.utils.ResultUtil;
import com.example.springboot.utils.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * Created by wuning at 2018/9/11 10:50
 **/
@RestController
@RequestMapping("/user/register")
public class RegisterController {

    @Autowired
    private UserJPA userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping(value="/name",method= RequestMethod.POST)
    public HttpResult registerByName(@RequestParam String username,@RequestParam String password,@RequestParam String match_password) {

        if(!ValidateUtil.get().matchUsername(username)){
            return ResultUtil.get().error(Constant.CODE_USERNAME_INVALIDATED, "UserName Invalidated");
        }

        User user0 = userRepository.findByUsernameCaseInsensitive(username);
        if (user0!=null){
            return ResultUtil.get().error(Constant.CODE_USER_EXIST, "User Exist");
        }

        if (!ValidateUtil.get().matchPassword(password)){
            return ResultUtil.get().error(Constant.CODE_PASSWORD_INVALIDATED, "Password Invalidated");
        }

        if (!password.equals(match_password)){
            return ResultUtil.get().error(Constant.CODE_PASSWORD_NOT_SAME, "Password Not Same");
        }


        //用户注册成功
        User user = new User();
        user.setUsername(username);
        user.setPassword(passwordEncoder.encode(password));
        user.setActivated(true);
        userRepository.save(user);
        return ResultUtil.get().success();
    }

    @RequestMapping(value="/verify/email",method=RequestMethod.POST)
    public HttpResult verifyByEmail(@RequestParam String email) {

        if(!ValidateUtil.get().matchEmail(email)){
            return ResultUtil.get().error(Constant.CODE_EMAIL_INVALIDATED, "Email Invalidated");
        }

        User user0 = userRepository.findByEmailCaseInsensitive(email);
        if (user0!=null){
            return ResultUtil.get().error(Constant.CODE_EMAIL_EXIST, "Email Exist");
        }

        //todo 1、新成或从数据库查找有效的code
        String code = "2428";
        //todo 2、发送邮件出去

        return ResultUtil.get().success(new VerifyCode(code));
    }

    @RequestMapping(value="/email",method=RequestMethod.POST)
    public HttpResult registerByEmail(@RequestParam String email,@RequestParam String password,@RequestParam String verify_code) {

        if(!ValidateUtil.get().matchEmail(email)){
            return ResultUtil.get().error(Constant.CODE_EMAIL_INVALIDATED, "Email Invalidated");
        }

        User user0 = userRepository.findByEmailCaseInsensitive(email);
        if (user0!=null){
            return ResultUtil.get().error(Constant.CODE_EMAIL_EXIST, "Email Exist");
        }

        if (!ValidateUtil.get().matchPassword(password)){
            return ResultUtil.get().error(Constant.CODE_PASSWORD_INVALIDATED, "Password Invalidated");
        }

        //todo 从数据库查询verify_code是否合法存在
        boolean checkCode = true;
        if (!checkCode){
            return ResultUtil.get().error(Constant.CODE_VERIFYCODE_INVALIDATED, "VerifyCode Invalidated");
        }


        //用户注册成功
        User user = new User();
        user.setUsername(UUID.randomUUID().toString());
        user.setEmail(email);
        user.setPassword(passwordEncoder.encode(password));
        user.setActivated(true);
        userRepository.save(user);
        return ResultUtil.get().success();
    }


    @RequestMapping(value="/verify/phone",method=RequestMethod.POST)
    public HttpResult verifyByPhone(@RequestParam String phone) {

        if(!ValidateUtil.get().matchPhone(phone)){
            return ResultUtil.get().error(Constant.CODE_PHONE_INVALIDATED, "Phone Invalidated");
        }

        User user0 = userRepository.findByPhone(phone);
        if (user0!=null){
            return ResultUtil.get().error(Constant.CODE_PHONE_EXIST, "Phone Exist");
        }

        //todo 1、新成或从数据库查找有效的code
        String code = "";
        //todo 2、发送短信出去

        return ResultUtil.get().success(new VerifyCode(code));
    }

    @RequestMapping(value="/phone",method=RequestMethod.POST)
    public HttpResult registerByPhone(@RequestParam String phone, @RequestParam String verify_code) {

        if(!ValidateUtil.get().matchPhone(phone)){
            return ResultUtil.get().error(Constant.CODE_PHONE_INVALIDATED, "Phone Invalidated");
        }

        User user0 = userRepository.findByPhone(phone);
        if (user0!=null){
            return ResultUtil.get().error(Constant.CODE_PHONE_EXIST, "Phone Exist");
        }

        //todo 从数据库查询verify_code是否合法存在
        boolean checkCode = true;
        if (!checkCode){
            return ResultUtil.get().error(Constant.CODE_VERIFYCODE_INVALIDATED, "VerifyCode Invalidated");
        }


        //用户注册成功
        User user = new User();
        user.setUsername(UUID.randomUUID().toString());
        user.setPhone(phone);
        user.setActivated(true);
        userRepository.save(user);
        return ResultUtil.get().success();

    }
}
